package com.chinairi.mall.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chinairi.common.annotation.DoPage;
import com.chinairi.common.utils.ResponseUtil;
import com.chinairi.mall.controller.viewobject.GoodsClassCodeVO;
import com.chinairi.mall.controller.viewobject.GoodsInfoVO;
import com.chinairi.mall.dao.GoodsInfoMapper;
import com.chinairi.mall.error.BusinessException;
import com.chinairi.mall.error.EmBusinessError;
import com.chinairi.mall.model.GoodsClassCode;
import com.chinairi.mall.dao.GoodsClassCodeMapper;
import com.chinairi.mall.model.GoodsInfo;
import com.chinairi.mall.service.IGoodsClassCodeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品分类编码表 服务实现类
 * </p>
 *
 * @author gsl
 * @since 2020-08-17
 */
@Service
public class GoodsClassCodeServiceImpl extends ServiceImpl<GoodsClassCodeMapper, GoodsClassCode> implements IGoodsClassCodeService {

    @Autowired
    private GoodsClassCodeMapper goodsClassCodeMapper;

    @Autowired
    private GoodsInfoMapper goodsInfoMapper;


    /**
     * 获取商品大类
     *
     * @return
     */
    @Override
    public List<GoodsClassCodeVO> getBigClass() {
        List<GoodsClassCode> goodsClassCodeList = goodsClassCodeMapper.getBigClass();
        List<GoodsClassCodeVO> goodsClassCodeVOList = JSON.parseArray(JSON.toJSONString(goodsClassCodeList), GoodsClassCodeVO.class);
        System.out.println(goodsClassCodeVOList);
        return goodsClassCodeVOList;
    }

    /**
     * 获取某一类下的子类
     *
     * @param goodsClassDbid
     * @return
     */
    @Override
    public List<GoodsClassCodeVO> getSubClass(String goodsClassDbid) {
        System.out.println("传入的商品分类编码id: " + goodsClassDbid);
        //查询出所有中类
        List<GoodsClassCode> goodsClassList = goodsClassCodeMapper.getSubClass(goodsClassDbid);
        List<GoodsClassCodeVO> goodsClassCodeVOList = JSON.parseArray(JSON.toJSONString(goodsClassList), GoodsClassCodeVO.class);
        for (GoodsClassCodeVO goodsClassCodeVO : goodsClassCodeVOList) {
            String dbid = goodsClassCodeVO.getGoodsClassDbid();
            List<GoodsClassCode> goodsSmallClassList = goodsClassCodeMapper.getSubClass(dbid);
            List<GoodsClassCodeVO> goodsClassCodeVOs = JSON.parseArray(JSON.toJSONString(goodsSmallClassList), GoodsClassCodeVO.class);
            goodsClassCodeVO.setGoodsClassCodeVOList(goodsClassCodeVOs);
        }
        return goodsClassCodeVOList;
    }

    /**
     * 根据商品分类编码(小类)查找下面的所有商品
     *
     * @param goodsClassDbid
     * @return
     */
    @Override
    @DoPage
    public List<GoodsInfoVO> getGoodsByClassCode(String goodsClassDbid) {
        QueryWrapper<GoodsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq(goodsClassDbid != null, "GOODS_CLASS_DBID", goodsClassDbid);
        List<GoodsInfo> goodsInfoList = goodsInfoMapper.selectList(queryWrapper);
        return JSON.parseArray(JSON.toJSONString(goodsInfoList), GoodsInfoVO.class);
    }

    /**
     * 所有商品分类
     *
     * @return
     */
    @Override
    public List<GoodsClassCodeVO> getAllClass() {
        List<GoodsClassCodeVO> goodsClassCodeVOList = getBigClass();
        for (GoodsClassCodeVO goodsClassCodeVO : goodsClassCodeVOList) {
            goodsClassCodeVO.setGoodsClassCodeVOList(getSubClass(goodsClassCodeVO.getGoodsClassDbid()));
        }
        return goodsClassCodeVOList;
    }

    /**
     * 获取所有商品分类，树形结构
     *
     * @return
     */
    @Override
    public List<GoodsClassCodeVO> getGoodsClass() {
        List<GoodsClassCodeVO> goodsClassCodeVOList = goodsClassCodeMapper.getGoodsClass();
        return goodsClassCodeVOList.stream().filter(vo -> vo.getGccClassLeave() == 1)
                .peek(vo -> vo.setGoodsClassCodeVOList(getChildList(vo,goodsClassCodeVOList)))
                .collect(Collectors.toList());
    }

    /**
     * 分级获取子类
     * @param goodsClassCodeVO
     * @param list
     * @return
     */
    private List<GoodsClassCodeVO> getChildList(GoodsClassCodeVO goodsClassCodeVO, List<GoodsClassCodeVO> list) {
        return list.stream().filter(vo -> vo.getGccParentId().equals(goodsClassCodeVO.getGoodsClassDbid()))
                .peek(vo -> vo.setGoodsClassCodeVOList(getChildList(vo, list)))
                .collect(Collectors.toList());
    }
}
